package com.itheima.listener;

import com.itheima.autoconfig.green.AliyunGreenTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MovementStateListener {

    @DubboReference
    MovementService movementService;

    @Autowired
    AliyunGreenTemplate aliyunGreenTemplate;

    @RabbitListener(queuesToDeclare = @Queue("tanhua.movement.state"))
    public void checkMovement(String movementId){
        //1.通过id获取动态详情
        Movement movement = movementService.findById(new ObjectId(movementId));

        //2.审核动态的文本和图片
        if (aliyunGreenTemplate.checkText(movement.getTextContent())
                && aliyunGreenTemplate.checkImage(movement.getMedias())) {

            //3.若审核通过,修改动态的状态为1;
            movement.setState(1);
        }else {
            //4.若审核为通过,修改动态的状态为2;
            movement.setState(2);
        }

        //5.更新动态
        movementService.update(movement);
    }

}
